package net.betou.cms.dao.assist.impl;

import java.util.List;

import net.betou.cms.dao.assist.CmsGuestbookDao;
import net.betou.cms.entity.assist.CmsGuestbook;
import net.betou.common.hibernate3.Finder;
import net.betou.common.hibernate3.HibernateBaseDao;
import net.betou.common.page.Pagination;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;


@Repository
public class CmsGuestbookDaoImpl extends
		HibernateBaseDao<CmsGuestbook, Integer> implements CmsGuestbookDao {
	
	public Pagination getPage( Integer siteId,  String queryForTitle, String queryForIp, String queryForUser, Integer ctgId, int pageNo, boolean cacheable, Boolean recommend,
			Boolean checked, int pageSize, Integer order ){
		Finder f = this.createFinder( siteId, ctgId, queryForTitle, queryForIp, queryForUser, order, recommend, checked, cacheable );
		return find(f, pageNo, pageSize);
	}
	
	public Pagination getPage(Integer siteId, Integer ctgId,Integer userId,Boolean recommend,
			Boolean checked, boolean asc, boolean cacheable, int pageNo,
			int pageSize) {
		Finder f = createFinder(siteId, ctgId, userId,recommend, checked, asc,
				cacheable);
		return find(f, pageNo, pageSize);
	}

	@SuppressWarnings("unchecked")
	public List<CmsGuestbook> getList(Integer siteId, Integer ctgId,
			Boolean recommend, Boolean checked, boolean desc,
			boolean cacheable, int first, int max) {
		Finder f = createFinder(siteId, ctgId, null,recommend, checked, desc,
				cacheable);
		f.setFirstResult(first);
		f.setMaxResults(max);
		return find(f);
	}
	
	private Finder createFinder(Integer siteId, Integer ctgId, String queryForTitle, String queryForIp, String queryForUser, Integer order, 
			Boolean recommend, Boolean checked, boolean cacheable) {
		Finder f = Finder.create("from CmsGuestbook bean where 1=1");
		if (siteId != null) {
			f.append(" and bean.site.id=:siteId");
			f.setParam("siteId", siteId);
		}
		if (ctgId != null) {
			f.append(" and bean.ctg.id=:ctgId");
			f.setParam("ctgId", ctgId);
		}
		
		if( StringUtils.isNotBlank( queryForTitle )){
			f.append( " and bean.ext.title like :queryForTitle" );
			f.setParam( "queryForTitle", "%" + queryForTitle + "%" );
		}
		
		if( StringUtils.isNotBlank( queryForIp )){
			f.append(" and bean.ip=:queryForIp");
			f.setParam("queryForIp", queryForIp);
		}
		
		if( StringUtils.isNotBlank( queryForUser )){
			f.append(" and bean.member.username=:queryForUser");
			f.setParam("queryForUser", queryForUser);
		}
	
		if (recommend != null  ) {
			f.append(" and bean.recommend=:recommend");
			f.setParam("recommend", recommend);
		}
		if (checked != null) {
			f.append(" and bean.checked=:checked");
			f.setParam("checked", checked);
		}
		f.append( getOrder( order ) );
		f.setCacheable(cacheable);
		return f;
	}
	
	private String getOrder( Integer order ){
		
		if( order == null || order == 0){
			return " order by bean.id desc ";
		}
		String orderHql;
		
		switch( order ){
			case 1:
				orderHql = " order by bean.createTime desc ";
				break;
			case 2:
				orderHql =  " order by bean.createTime asc";
				break;
			default:
				orderHql = " order by bean.id desc ";
				break;
		}
		return orderHql;
	}
	
	private Finder createFinder(Integer siteId, Integer ctgId,Integer userId,
			Boolean recommend, Boolean checked, boolean desc, boolean cacheable) {
		Finder f = Finder.create("from CmsGuestbook bean where 1=1");
		if (siteId != null) {
			f.append(" and bean.site.id=:siteId");
			f.setParam("siteId", siteId);
		}
		if (ctgId != null) {
			f.append(" and bean.ctg.id=:ctgId");
			f.setParam("ctgId", ctgId);
		}
		if (userId != null) {
			f.append(" and bean.member.id=:userId");
			f.setParam("userId", userId);
		}
		if (recommend != null) {
			f.append(" and bean.recommend=:recommend");
			f.setParam("recommend", recommend);
		}
		if (checked != null) {
			f.append(" and bean.checked=:checked");
			f.setParam("checked", checked);
		}
		if (desc) {
			f.append(" order by bean.id desc");
		} else {
			f.append(" order by bean.id asc");
		}
		f.setCacheable(cacheable);
		return f;
	}

	public CmsGuestbook findById(Integer id) {
		CmsGuestbook entity = get(id);
		return entity;
	}

	public CmsGuestbook save(CmsGuestbook bean) {
		getSession().save(bean);
		return bean;
	}

	public CmsGuestbook deleteById(Integer id) {
		CmsGuestbook entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

	@Override
	protected Class<CmsGuestbook> getEntityClass() {
		return CmsGuestbook.class;
	}
}